<template>
  <div class="app-container">
    <div>
      <p class="dialogTitle title">当前地震事件: 
        <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
        <span v-else>请在地震事件管理中设置当前地震事件</span>
      </p>
    </div>

    <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
            <el-button type="primary" :disabled="!currentEvent" @click="handleAdd">新建</el-button>
        </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" stripe>
        <el-table-column label="序号" align="center" width="55">
          <template #default="scope">
            <span>{{(queryParams.page - 1) * queryParams.rows + scope.$index + 1}}</span>
          </template>             
        </el-table-column>
        <el-table-column label="指挥部编号" align="center" prop="headCode">
          <template #default="scope">
            <span style="color: #107ce1;cursor: pointer;" @click="handleEdit(scope.row)">{{ scope.row.headCode }}</span>
          </template>          
        </el-table-column>
        <el-table-column label="指挥部名称" align="center" prop="headName" width="150px"/>
        <el-table-column label="指挥长" align="center" prop="leader" />
        <el-table-column label="联系方式" align="center" prop="tel" />
        <el-table-column label="创建单位" align="center" prop="createUnit" />
        <el-table-column label="创建人" align="center" prop="createUserName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">     
			      <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"   @pagination="getList" /> 
      
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="addRef" label-width="120px" :model="form" :rules="rules">
        <el-row style="margin: 20px 0;">        
            <el-col>
              <el-form-item label="指挥部编号" prop="headCode" required>
                <el-input v-model="form.headCode" placeholder="请输入指挥部编号"  disabled />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="指挥部名称" prop="headName">
                <el-input v-model="form.headName" placeholder="请输入指挥部名称"  clearable />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="指挥长" prop="leader">
                <el-input v-model="form.leader" placeholder="请选择指挥长" :suffix-icon="MoreFilled"  readonly @click="selectLeader" />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="联系方式" prop="tel">
                <el-input v-model="form.tel" placeholder="请输入联系方式"  disabled />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="描述信息" prop="commentInfo">
                <el-input v-model="form.commentInfo" type="textarea" maxlength="500" show-word-limit :autosize="{ minRows: 5, maxRows: 5 }" placeholder="请输入描述信息" clearable/>
              </el-form-item>
            </el-col>            
            <el-col>
              <el-form-item label="创建人单位" prop="createUnit" required>
                <el-input v-model="form.createUnit" placeholder="请输入创建人单位"  disabled />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="创建人" prop="createUserName" required>
                <el-input v-model="form.createUserName" placeholder="请输入创建人"  disabled />
              </el-form-item>
            </el-col>            
            <el-col>
              <el-form-item label="创建时间" prop="createTime" required>
                <el-input v-model="form.createTime" placeholder="请输入创建时间"  disabled />
              </el-form-item>
            </el-col>

        </el-row>         
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="save">保 存</el-button>
              <el-button @click="open = false">取 消</el-button>
          </div>
      </template>
    </el-dialog>   
    <el-dialog title="选择指挥长" v-model="openLeader" width="600px" append-to-body>
      <p></p>
      <el-table  ref="singleTableRef" :data="dataList1" stripe>
            <el-table-column label="单选" width="55px">
              <template #default="scope">
                <el-radio :label="scope.row.xpimId" v-model="selectRadio" @click.native.stop.prevent="getCurrentRow(scope.row)"
                  >&nbsp;</el-radio>
              </template>
            </el-table-column>
            <el-table-column label="序号" align="center" width="55">
              <template #default="scope">
                <span>{{(queryParamsForm.page - 1) * queryParamsForm.rows + scope.$index + 1}}</span>
              </template>             
            </el-table-column>
            <el-table-column label="名字" align="center" prop="personName" />
            <el-table-column label="电话" align="center" prop="tel" />
      </el-table>
      <pagination v-show="total1 > 0" :total="total1" v-model:page="queryParamsForm.page" v-model:limit="queryParamsForm.rows"   @pagination="getList1" />  
      <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="querySelect" :disabled="!forbidden">确 定</el-button>
              <el-button @click="openLeader = false">取 消</el-button>
          </div>
      </template>
    </el-dialog>        
  </div>
</template>

<script setup name="headquarters">
import useEventStore from "@/store/modules/event";
import { getAllList, getAllList1, saveData, getDetail, updateData ,getHeadCode, del } from "@/api/comprehensiveInformation/headquarters.js"
import { MoreFilled } from '@element-plus/icons-vue'
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const dataList1 = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const total1 = ref(0);
const showSearch = ref(true);
const open = ref(false);
const openLeader = ref(false);
const title = ref();
const selectionData = ref({});
const selectRadio = ref('');
const forbidden = ref(true);
const currentEvent = computed(() => useEventStore().currentEvent);
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
    },
    form: {
    },
    queryParamsForm: {
        page: 1,
        rows: 10,
        magnitude: undefined,
    },
    rules: {
      headName:[{ required: true, message: "指挥部名称不能为空", trigger: "blur" }],
      leader:[{ required: true, message: "指挥长不能为空", trigger: "change" }],
      tel:[{ required: true, message: "手机号不能为空",trigger: "change"},{
            validator: function(rule, value, callback) {
              if (/^1[34578]\d{9}$/.test(value) == false) {
                callback(new Error("手机号格式错误"));
              } else {
                callback();
              }
            },trigger: "blur"
          }]
    }
})
const { queryParams,queryParamsForm, form, rules } = toRefs(data)

function  getCurrentRow (row) {
    if (!forbidden.value) {
      forbidden.value = true
    }
    if (selectRadio.value) {
      if (selectRadio.value == row.xpimId) {
        selectRadio.value = ''
        forbidden.value = false
        setTimeout(() => {
          forbidden.value = true
        }, 0)
        selectionData.value = {};// 取消勾选就把回传数据清除
        return
      } else {
        forbidden.value = false
        setTimeout(() => {
          forbidden.value = true
        }, 0)
      }
    } else {
      forbidden.value = false
      setTimeout(() => {
        forbidden.value = true
      }, 0)
    }
    selectRadio.value = row.xpimId;
    selectionData.value = row;
    console.log(selectionData.value,'222');
  }

function selectLeader(){
  openLeader.value = true;
  selectRadio.value = '';
  forbidden.value = false;
  selectionData.value = {};
}
function querySelect(){
  form.value.leader = selectionData.value.personName;
  form.value.tel = selectionData.value.tel;
  form.value.leaderId = selectionData.value.xpimId;
  openLeader.value = false;
}

// 获取getHeadCode信息
function getHeaderData(){
  getHeadCode({eventCode:currentEvent.value.eventCode}).then(res=>{
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
    console.log(res.data,'lalal');
  })
}

// 新增
function handleAdd(){
  title.value = '新建指挥部';
  getHeaderData()

}
// 编辑
function handleEdit(row){
  title.value = '编辑指挥部'

  detail(row.xhmId);
}
// 查看
function detail(id){
  getDetail({id:id}).then(res=>{
    open.value = true;
    proxy.resetForm("addRef");
    form.value = res.data;
  })
}
// 保存
function save(){
  proxy.$refs["addRef"].validate(valid => {
    if (valid) {
      if(title.value=='新建指挥部'){
        saveData({...form.value,xsemId:currentEvent.value.id}).then(res=>{
          proxy.$modal.msgSuccess("新建成功");
          open.value = false;
          getList();
        })
      }else{
        updateData({...form.value}).then(res=>{
          proxy.$modal.msgSuccess("保存成功");
          open.value = false;
          getList();
        })
      }
    }
  })

}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
			.confirm("是否确认删除该信息，请确认是否要删除？", )
			.then(function() {
				return del(row.xhmId);
			})
			.then(() => {
				getList();
				proxy.$modal.msgSuccess("删除成功");
			})
			.catch(() => {});
}

/** 查询数据列表 */
function getList() {
  if(currentEvent.value){
    loading.value = true;
    getAllList({...queryParams.value,id:currentEvent.value.id}).then(response => {
        dataList.value = response.data.rows ? response.data.rows : [];
        total.value = response.data.total;
        loading.value = false;
    }).catch(err=>{loading.value = false});
    getList1();
  }else{
    dataList.value = [];
    total.value = 0;
  }
}

function getList1() {
    getAllList1({...queryParamsForm.value,xsemId:currentEvent.value.id}).then(response => {
        dataList1.value = response.data.rows ? response.data.rows : [];
        total1.value = response.data.total;
    });
}


onMounted(() => {
  setTimeout(()=>{
    getList();
  },200)
})
</script>

<style scoped lang="scss">
.title{
  margin: 0 0 20px!important;
}
</style>

